package com.winhxd.b2c.common.feign.product;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.constant.ServiceName;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.product.condition.*;
import com.winhxd.b2c.common.domain.product.model.ProductBrand;
import com.winhxd.b2c.common.domain.product.model.ProductSpu;
import com.winhxd.b2c.common.domain.product.vo.*;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

/**
 * 商品服务
 */
@FeignClient(value = ServiceName.PRODUCT_SERVICE, fallbackFactory = ProductServiceFallback.class)
public interface ProductServiceClient {

    /**
     * @description Web端获取商品列表
     * @auther: yuluyuan
     * @date: 2019/1/7 19:04
     * @param: [condition]
     * @return: com.winhxd.b2c.common.domain.ResponseResult<java.util.List<com.winhxd.b2c.common.domain.product.vo.ProductListWebVO>>
     */
	@RequestMapping(value = "/product/8100/v1/findProductListByPage", method = RequestMethod.POST)
	ResponseResult<PagedList<ProductListWebVO>> findProductListByPage(@RequestBody ProductCondition condition);

	/**
	 * @description Web端-通用商品列表
	 * @auther: yuluyuan
	 * @date: 2019/1/7 19:04
	 * @param: [model]
	 * @return: com.winhxd.b2c.common.domain.ResponseResult<java.lang.Integer>
	 */
	@RequestMapping(value = "/product/8101/v1/findProductList", method = RequestMethod.POST)
	ResponseResult<List<ProductSpu>> findProductList(@RequestBody ProductSpu model);

	/**
	 * @description 批量操作商品
	 * @auther: yuluyuan
	 * @date: 2019/1/7 19:05
	 * @param: [condition]
	 * @return: com.winhxd.b2c.common.domain.ResponseResult<java.lang.Integer>
	 */
	@RequestMapping(value = "/product/8102/v1/operateProducts", method = RequestMethod.POST)
	ResponseResult<Integer> operateProducts(@RequestBody ProductCondition condition);

    /**
     * @description Web端获取商品列表(回收站)
     * @auther: yuluyuan
     * @date: 2019/1/7 19:04
     * @param: [condition]
     * @return: com.winhxd.b2c.common.domain.ResponseResult<java.util.List<com.winhxd.b2c.common.domain.product.vo.ProductListWebVO>>
     */
    @RequestMapping(value = "/product/8103/v1/findProductList", method = RequestMethod.POST)
    ResponseResult<PagedList<ProductListWebVO>> findProductListForRecycleBin(@RequestBody ProductCondition condition);

    /**
     * 获取购物车商品sku列表
     * @param condition
     * @return Map.key 为spuId
     */
    @RequestMapping(value = "/product/8104/v1/findShopcarSkuList", method = RequestMethod.POST)
    ResponseResult<Map<Long, ProductShopcarSkuVO>> findShopcarSkuList(@RequestBody ProductSkuListCondition condition);

    /**
     * 获取收藏夹商品信息列表
     * @param condition
     * @return Map.key 为spuId
     */
    @RequestMapping(value = "/product/8105/v1/findSimpleProductList", method = RequestMethod.POST)
    ResponseResult<Map<Long, ProductSpuSimpleVO>> findSimpleProductList(@RequestBody ProductSpuListCondition condition);

    /**
     * @description 保存商品信息
     * @auther yuluyuan
     * @date: 2019/1/8 17:04
     * @param: [condition]
     * @return: com.winhxd.b2c.common.domain.ResponseResult<java.lang.Integer>
     */
    @RequestMapping(value = "/product/8106/v1/saveProduct", method = RequestMethod.POST)
    public ResponseResult<Integer> saveProduct(@RequestBody ProductVO condition);

    /**
     * WEB端—新增、编辑商品
     * @param condition
     * @return
     */
    @RequestMapping(value = "product/8107/v1/addOrModifyProduct", method = RequestMethod.POST)
    ResponseResult<ProductVO> addOrModifyProduct(@RequestBody ProductSaveOrUpdateCondition condition);


    @RequestMapping(value = "product/8108/v1/selectAllBrand", method = RequestMethod.POST)
    ResponseResult<List<ProductBrand>> selectAllBrand();

    /**
     * @description Web端-获取商品列表(商城组件，带图片)
     * @auther: yuluyuan
     * @date: 2019/1/7 19:04
     * @param: [condition]
     * @return: com.winhxd.b2c.common.domain.ResponseResult<java.util.List<com.winhxd.b2c.common.domain.product.vo.ProductListWebVO>>
     */
    @RequestMapping(value = "/product/8109/v1/findProductListForAssemblyByPage", method = RequestMethod.POST)
    ResponseResult<PagedList<ProductListForAssemblyWebVO>> findProductListForAssemblyByPage(@RequestBody ProductCondition condition);

    /**
     *
     * @Author cuiyueqiang
     * @Description 创建订单减库存增销量
     * @Date 2019/1/14 17:37
     * @param condition
     * @return
     **/
    @RequestMapping(value = "product/8110/v1/operateSubProductStock", method = RequestMethod.POST)
    ResponseResult<Integer> operateSubProductStock(@RequestBody List<ProductStockSkuCondition> condition);


    /** 
     *
     * @Author cuiyueqiang
     * @Description 取消订单增库存减销量
     * @Date 2019/1/16 11:28 
     * @param condition
     * @return 
     **/
    @RequestMapping(value = "product/8111/v1/operateAddProductStock", method = RequestMethod.POST)
    ResponseResult<Integer> operateAddProductStock(@RequestBody List<ProductStockSkuCondition> condition);

    /**
     * 更新上架商品在ES的：商品的价格方案以及商品基本信息
     * @return
     */
    @RequestMapping(value = "product/8112/v1/operateProducPriceAndInfo", method = RequestMethod.POST)
    ResponseResult<Boolean> operateProducPriceAndInfo();
}

@Component
class ProductServiceFallback implements ProductServiceClient, FallbackFactory<ProductServiceClient> {

    private static final Logger logger = LoggerFactory.getLogger(ProductServiceFallback.class);

    private Throwable throwable;

    public ProductServiceFallback(Throwable throwable) {
		this.throwable = throwable;
    }

    public ProductServiceFallback() {
    }


	@Override
	public ProductServiceClient create(Throwable throwable) {
		return new ProductServiceFallback(throwable);
	}

	@Override
	public ResponseResult<PagedList<ProductListWebVO>> findProductListByPage(ProductCondition condition) {
    	logger.error("查询商品列表接口调用失败,商品中心:8100");
		ResponseResult<PagedList<ProductListWebVO>> responseResult = new ResponseResult<>();
		responseResult.setCode(BusinessCode.CODE_1001);
		return responseResult;
	}

    @Override
    public ResponseResult<PagedList<ProductListForAssemblyWebVO>> findProductListForAssemblyByPage(ProductCondition condition) {
        logger.error("Web端-获取商品列表(商城组件，带图片)接口调用失败,商品中心:8100");
        ResponseResult<PagedList<ProductListForAssemblyWebVO>> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }

    @Override
    public ResponseResult<List<ProductSpu>> findProductList(ProductSpu model) {
        logger.error("Web端-通用商品列表接口调用失败,商品中心:8101");
        ResponseResult<List<ProductSpu>> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }

    @Override
    public ResponseResult<Integer> operateProducts(ProductCondition condition) {
        logger.error("批量操作商品接口调用失败,商品中心:8102");
        ResponseResult<Integer> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }

    @Override
    public ResponseResult<PagedList<ProductListWebVO>> findProductListForRecycleBin(ProductCondition condition) {
        logger.error("查询商品列表(回收站)接口调用失败,商品中心:8103");
        ResponseResult<PagedList<ProductListWebVO>> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }


    @Override
    public ResponseResult<Map<Long, ProductShopcarSkuVO>> findShopcarSkuList(ProductSkuListCondition condition) {
        logger.error("获取购物车商品sku列表接口调用失败,商品中心:8104");
        ResponseResult<Map<Long, ProductShopcarSkuVO>> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }

    @Override
    public ResponseResult<Map<Long, ProductSpuSimpleVO>> findSimpleProductList(ProductSpuListCondition condition) {
        logger.error("获取收藏夹商品信息列表接口调用失败,商品中心:8105");
        ResponseResult<Map<Long, ProductSpuSimpleVO>> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }

    @Override
    public ResponseResult<Integer> saveProduct(ProductVO condition) {
        logger.error("保存商品信息接口调用失败,商品中心:8106");
        ResponseResult<Integer> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }

    @Override
    public ResponseResult<ProductVO> addOrModifyProduct(ProductSaveOrUpdateCondition condition) {

        logger.error("WEB端—新增、编辑商品接口调用失败,商品中心:8107");
        ResponseResult<ProductVO> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }


    @Override
    public ResponseResult<List<ProductBrand>> selectAllBrand() {
        logger.error("查询所有的商品品牌:8108");
        ResponseResult<List<ProductBrand>> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }


    @Override
    public ResponseResult<Integer> operateSubProductStock(List<ProductStockSkuCondition> condition) {
        logger.error("商品库存及销量增减接口调用失败,商品中心:8110");
        ResponseResult<Integer> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }

    @Override
    public ResponseResult<Integer> operateAddProductStock(List<ProductStockSkuCondition> condition) {
        logger.error("商品库存及销量增减接口调用失败,商品中心:8111");
        ResponseResult<Integer> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }

    @Override
    public ResponseResult<Boolean> operateProducPriceAndInfo() {
        logger.error("更新上架商品在ES的：商品的价格方案以及商品基本信息接口调用失败,商品中心:8112");
        ResponseResult<Boolean> responseResult = new ResponseResult<>();
        responseResult.setCode(BusinessCode.CODE_1001);
        return responseResult;
    }
}
